package com.yssw.account.domain;

import com.yssw.account.service.CalculationService;
import lombok.Getter;

/**
 * @Author KnockingFarmers
 * @Date 2023/4/15 14:35
 * @PackageName:com.yssw.account.domain
 * @ClassName: AccountTypeEnums
 * @Description: TODO
 * @Version 1.0
 */
@Getter
public enum AccountTypeEnums {

    /**
     * 支出
     */
    OUT(1, (balance, money) -> {
        return balance.subtract(money);
    }),


    /**
     * 收入
     */
    ADD(2, (balance, money) -> {
        return  balance.add(money);
    }),

    /**
     * 收款
     */
    COLLECT(3, (balance, money) -> {
        return  balance.add(money);
    }),

    /**
     * 付款
     */
    PAY(4, (balance, money) -> {
        return balance.subtract(money);
    });


    private int val;
    private CalculationService calculationService;

    AccountTypeEnums(int val,  CalculationService calculationService) {
        this.val = val;
        this.calculationService = calculationService;

    }

    /**
     * 提前判断，用于解决
     * Case中出现的Constant expression required
     * @param type
     * @return
     */
    public static CalculationService getCalculation(int type) {
        for (AccountTypeEnums x : values()) {
            if (type==x.getVal()) {
                return x.getCalculationService();
            }
        }
        return null;
    }

}
